package com.sumer.psychology.sharing.domain.model;

import com.sumer.psychology.common.Assert;
import com.sumer.psychology.common.domain.model.IdentifiedValueObject;
import com.sumer.psychology.friend.domain.model.MemberId;

import java.util.Date;

/**
 * 一个资源推荐
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/9/12
 * Time: 9:44
 */
public class Recommendation extends IdentifiedValueObject {
    private SharedResourceId sharedResourceId;
    private MemberId from;
    private MemberId to;
    private Date createTime;

    /**
     * 创建推荐
     *
     * @param sharedResourceId id
     * @param from from
     * @param to to
     */
    public Recommendation(SharedResourceId sharedResourceId, MemberId from, MemberId to) {
        setSharedResourceId(sharedResourceId);
        setFrom(from);
        setTo(to);
        setCreateTime(new Date());
    }

    public Recommendation() {
    }

    public void setSharedResourceId(SharedResourceId sharedResourceId) {
        Assert.notNull(sharedResourceId, "resource id不能为空");
        Assert.notEmpty(sharedResourceId.identity(), "resource id不能为空");

        this.sharedResourceId = sharedResourceId;
    }

    public void setFrom(MemberId from) {
        Assert.notNull(from, "发布者id不能为空");
        Assert.notEmpty(from.identity(), "发布者id不能为空");

        this.from = from;
    }

    public void setTo(MemberId to) {
        Assert.notNull(to, "朋友id不能为空");
        Assert.notEmpty(to.identity(), "朋友id不能为空");

        this.to = to;
    }

    public void setCreateTime(Date createTime) {
        Assert.notNull(createTime, "创建日期不能为空");

        this.createTime = createTime;
    }

    public SharedResourceId sharedResourceId() {
        return sharedResourceId;
    }

    public MemberId from() {
        return from;
    }

    public MemberId to() {
        return to;
    }

    public Date createTime() {
        return createTime;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Recommendation that = (Recommendation) o;

        if (from != null ? !from.equals(that.from) : that.from != null) return false;
        if (sharedResourceId != null ? !sharedResourceId.equals(that.sharedResourceId) : that.sharedResourceId != null)
            return false;
        if (to != null ? !to.equals(that.to) : that.to != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = sharedResourceId != null ? sharedResourceId.hashCode() : 0;
        result = 31 * result + (from != null ? from.hashCode() : 0);
        result = 31 * result + (to != null ? to.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Recommendation{" +
                "sharedResourceId=" + sharedResourceId +
                ", from=" + from +
                ", to=" + to +
                ", createTime=" + createTime +
                '}';
    }
}
